package HomeWork20241101;

import java.util.Arrays;
import java.util.Comparator;

// 1. 定义Swimmable接口
interface Swimmable {
    void swim();
}

// 2. 定义Flyable接口
interface Flyable {
    void fly();
}

// 3. 定义Animals抽象类
abstract class Animals {
    abstract void eat();
}

// 4. 定义WildDuck类实现接口并继承抽象类
class WildDuck extends Animals implements Swimmable, Flyable {
    @Override
    public void swim() {
        System.out.println("WildDuck is swimming.");
    }

    @Override
    public void fly() {
        System.out.println("WildDuck is flying.");
    }

    @Override
    void eat() {
        System.out.println("WildDuck is eating.");
    }
}

// 1. 定义Employee类实现Comparable接口
class Employee implements Comparable<Employee> {
    int id;
    int age;

    public Employee(int id, int age) {
        this.id = id;
        this.age = age;
    }

    @Override
    public int compareTo(Employee other) {
        return Integer.compare(this.id, other.id);
    }

    @Override
    public String toString() {
        return "Employee{" + "id=" + id + ", age=" + age + '}';
    }
}

// 3. 定义AgeComparator类实现Comparator接口
class AgeComparator implements Comparator<Employee> {
    @Override
    public int compare(Employee e1, Employee e2) {
        // 为了按照年龄从大到小排序，我们返回相反的比较结果
        return Integer.compare(e2.age, e1.age);
    }
}

public class Work241101
{
    public static void main1(String[] args) {
        // 创建WildDuck实例
        WildDuck wildDuck = new WildDuck();

        // 调用WildDuck的各种方法
        wildDuck.swim();
        wildDuck.fly();
        wildDuck.eat();

        // 6. 将WildDuck实例赋值给不同的引用并测试方法调用
        Swimmable swimmable = wildDuck;
        swimmable.swim(); // 应该可以调用

        Flyable flyable = wildDuck;
        flyable.fly(); // 应该可以调用

        Animals animal = wildDuck;
        animal.eat(); // 应该可以调用
    }


    public static void main(String[] args) {
        // 2. 在main()方法中创建Employee数组并排序
        Employee[] employees = {
                new Employee(3, 30),
                new Employee(1, 25),
                new Employee(2, 35)
        };

        // 使用Arrays.sort()方法对数组元素按id排序
        Arrays.sort(employees);
        System.out.println("Employees sorted by id:");
        for (Employee emp : employees) {
            System.out.println(emp);
        }

        // 使用AgeComparator对数组元素按年龄排序
        Arrays.sort(employees, new AgeComparator());
        System.out.println("\nEmployees sorted by age (descending):");
        for (Employee emp : employees) {
            System.out.println(emp);
        }
    }
}